package com.gy.api.pc.clap.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.EasyExcel;
import com.gy.api.base.Constants;
import com.gy.api.base.R;
import com.gy.api.base.Type;
import com.gy.api.common.file.domain.vo.FileVO;
import com.gy.api.common.file.service.IFileService;
import com.gy.api.mobile.company.dao.CompanyInfoDao;
import com.gy.api.mobile.company.domain.po.CompanyInfoPO;
import com.gy.api.mobile.msg.dao.UserMessagesDao;
import com.gy.api.pc.clap.dao.AssociationClapCulvertDao;
import com.gy.api.pc.clap.dao.AssociationClapDetailDao;
import com.gy.api.pc.clap.domain.po.AssociationClapCulvertPO;
import com.gy.api.pc.clap.domain.po.AssociationClapDetailPO;
import com.gy.api.pc.clap.domain.vo.*;
import com.gy.api.pc.clap.service.IAssociationClapCulvertService;
import com.gy.api.pc.examine.dao.ExamineDao;
import com.gy.api.pc.examine.dao.ExamineLogDao;
import com.gy.api.pc.examine.domain.po.ExamineLogPO;
import com.gy.api.pc.examine.domain.po.ExaminePO;
import com.gy.api.pc.examine.domain.vo.*;
import com.gy.api.pc.examine.service.IExamineService;
import com.gy.api.pc.sys.user.dao.SysUserDao;
import com.gy.api.pc.sys.user.domain.po.SysUserPO;
import com.gy.exception.BusinessException;
import com.gy.task.UserMessageThread;
import com.gy.util.ExcelDataListener;
import com.gy.util.ExcelImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.PictureData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.rmi.ServerException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class AssociationClapCulvertServiceImpl implements IAssociationClapCulvertService {
    @Autowired
    ExamineDao examineDao;
    @Autowired
    ExamineLogDao examineLogDao;
    @Autowired
    private IExamineService examineService;
    @Autowired
    AssociationClapCulvertDao associationClapCulvertDao;
    @Autowired
    AssociationClapDetailDao associationClapDetailDao;
    @Autowired
    private CompanyInfoDao companyInfoDao;
    @Override
    public void saveAndCommit(CulvertDto dto) {
       if( StringUtils.isNotBlank(dto.getId())){
           //修改数据
           AssociationClapCulvertPO associationClapCulvertPO=associationClapCulvertDao.selectByPrimaryKey(dto.getId());
           if(associationClapCulvertPO==null){
               throw new BusinessException(Type.FAIL.getCode(),"协拍函信息未找到！");
           }
           BeanUtils.copyProperties(dto, associationClapCulvertPO);
           associationClapCulvertPO.preUpdate();
           associationClapCulvertDao.updateByPrimaryKeySelective(associationClapCulvertPO);

           //处理协拍场景
           Example example1=new Example(AssociationClapDetailPO.class);
           example1.and().andEqualTo("culvertId",associationClapCulvertPO.getId());
           associationClapDetailDao.deleteByExample(example1);
            for (ClapDetailDto clapDetailDto:dto.getClapDetailDtos()){
                AssociationClapDetailPO clapDetailPO=new AssociationClapDetailPO();
                BeanUtils.copyProperties(clapDetailDto, clapDetailPO);
                clapDetailPO.preInsert();
                clapDetailPO.setCulvertId(associationClapCulvertPO.getId());
                associationClapDetailDao.insertSelective(clapDetailPO);
            }
           if(dto.getIsOutbox().equals("0")){
               //修改流程
               Example examineExample=new Example(ExaminePO.class);
               examineExample.and().andEqualTo("relatedId",associationClapCulvertPO.getId());
               ExaminePO examinePO= examineDao.selectOneByExample(examineExample);
               examinePO.setExamineState(1);
               examinePO.setCreateDate(new Date());
               examinePO.preUpdate();
               examineDao.updateByPrimaryKeySelective(examinePO);
               CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
               examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), Constants.EXAMINE_LOG_AGREE,Constants.EXAMINE_ONE_NODE,companyInfoPO.getUserName()
                       ,companyInfoPO.getId(),"提交审核",null,null,examinePO.getId()));
           }
        }else {
           //添加数据
           AssociationClapCulvertPO associationClapCulvertPO=new AssociationClapCulvertPO();
           BeanUtils.copyProperties(dto, associationClapCulvertPO);
           associationClapCulvertPO.preInsert();
           associationClapCulvertDao.insertSelective(associationClapCulvertPO);
           for (ClapDetailDto clapDetailDto:dto.getClapDetailDtos()){
               AssociationClapDetailPO clapDetailPO=new AssociationClapDetailPO();
               BeanUtils.copyProperties(clapDetailDto, clapDetailPO);
               clapDetailPO.preInsert();
               clapDetailPO.setCulvertId(associationClapCulvertPO.getId());
               associationClapDetailDao.insertSelective(clapDetailPO);
           }

           //创建一个新的流程任务
           CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
           examineService.addExamineState(new ExamineAddVO(associationClapCulvertPO.getId(), Constants.EXAMINE_COOPERATION_LETTER[1],Constants.EXAMINE_3_USER,Constants.EXAMINE_TYPE_COOPERATION_LETTER
                   ,companyInfoPO.getId(),companyInfoPO.getUserName()),dto.getIsOutbox().equals("0")?1:0);
       }
    }

    @Override
    public void delCulvert(String id) {
        //删除数据
        AssociationClapCulvertPO associationClapCulvertPO=associationClapCulvertDao.selectByPrimaryKey(id);
        if(associationClapCulvertPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"协拍函信息未找到！");
        }

        Example example=new Example(ExaminePO.class);
        example.and().andEqualTo("relatedId",associationClapCulvertPO.getId()).andEqualTo("examineState",0);
        examineDao.deleteByExample(example);

        Example example1=new Example(AssociationClapDetailPO.class);
        example1.and().andEqualTo("culvertId",associationClapCulvertPO.getId());
        associationClapDetailDao.deleteByExample(example1);
        associationClapCulvertDao.deleteByPrimaryKey(associationClapCulvertPO.getId());
    }


    @Override
    public CulvertDetailVO detailCulvert(String id) {
        CulvertDetailVO culvertDetailVO=new CulvertDetailVO();
//        Example exampleEx=new Example(ExaminePO.class);
//        exampleEx.and().andEqualTo("relatedId",id);
//        ExaminePO examinePO=examineDao.selectOneByExample(exampleEx);
//        culvertDetailVO.setFlowId(examinePO.getId());

        //查询详情
        AssociationClapCulvertPO associationClapCulvertPO=associationClapCulvertDao.selectByPrimaryKey(id);
        if(associationClapCulvertPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"协拍函信息未找到！");
        }
        BeanUtils.copyProperties(associationClapCulvertPO, culvertDetailVO);
        Example example1=new Example(AssociationClapDetailPO.class);
        example1.and().andEqualTo("culvertId",associationClapCulvertPO.getId());
        List<AssociationClapDetailPO> list=associationClapDetailDao.selectByExample(example1);
        List<ClapDetailDto> clapDetails =new ArrayList<>();
        if(list!=null&&list.size()>0){
            for (AssociationClapDetailPO associationClapDetailPO:list){
                ClapDetailDto clapDetailDto=new ClapDetailDto();
                BeanUtils.copyProperties(associationClapDetailPO, clapDetailDto);
                clapDetailDto.setHarmonizeState(associationClapDetailPO.getHarmonizeState());
                clapDetails.add(clapDetailDto);
            }
        }
        culvertDetailVO.setClapDetails(clapDetails);
        Example examineExample=new Example(ExaminePO.class);
        examineExample.and().andEqualTo("relatedId",associationClapCulvertPO.getId());
        ExaminePO examinePO= examineDao.selectOneByExample(examineExample);
        culvertDetailVO.setFlowState(examinePO.getExamineState());
        culvertDetailVO.setFlowId(examinePO.getId());
        return culvertDetailVO;
    }

    @Override
    public List<PageCulvertApprovalVO> getApprovalPage(PageCulvertSearchVO searchVO) {
        return examineDao.getApprovalPage(searchVO);
    }

    @Override
    public CulvertDetailVO getH5ApprovalDetail(String id) {

        CulvertDetailVO culvertDetailVO=this.detailCulvert(id);
        Example example=new Example(CompanyInfoPO.class);
        example.and().andEqualTo("companyName",culvertDetailVO.getProductionUnit());
        CompanyInfoPO companyInfoPO=companyInfoDao.selectOneByExample(example);
        culvertDetailVO.setBusinessLicense(companyInfoPO.getBusinessLicense());
        culvertDetailVO.setLegalPersonName(companyInfoPO.getUserName());
        culvertDetailVO.setLegalPersonPhone(companyInfoPO.getUserPhone());
        culvertDetailVO.setCompanyName(companyInfoPO.getCompanyName());
        return culvertDetailVO;
    }

    @Override
    public CulvertDetailVO getApprovalDetail(String id) {
        ExaminePO examinePO= examineDao.selectByPrimaryKey(id);

        CulvertDetailVO culvertDetailVO=this.detailCulvert(examinePO.getRelatedId());
        culvertDetailVO.setListOne(examineDao.getApprovalOne(id));
        Example example=new Example(CompanyInfoPO.class);
        example.and().andEqualTo("companyName",culvertDetailVO.getProductionUnit());
        CompanyInfoPO companyInfoPO=companyInfoDao.selectOneByExample(example);
        culvertDetailVO.setBusinessLicense(companyInfoPO.getBusinessLicense());
        culvertDetailVO.setLegalPersonName(companyInfoPO.getUserName());
        culvertDetailVO.setLegalPersonPhone(companyInfoPO.getUserPhone());
        culvertDetailVO.setCompanyName(companyInfoPO.getCompanyName());


        //查询审核记录
        Example example1=new Example(ExamineLogPO.class);
        example1.and().andEqualTo("examineId",examinePO.getId());
        example1.setOrderByClause("examine_date desc");
        List<ExamineLogPO> examineLogPOS=examineLogDao.selectByExample(example1);
        List<ExamineLogVO> examineLogVOList=new ArrayList<>();
        for (ExamineLogPO examineLogPO:examineLogPOS){
            ExamineLogVO examineLogVO=new ExamineLogVO();
            BeanUtils.copyProperties(examineLogPO, examineLogVO);
            examineLogVOList.add(examineLogVO);
        }
        culvertDetailVO.setExamineLogList(examineLogVOList);

        //查询提交记录
        List<SubmitLogVO> submitLogVOS=examineDao.getClapSubmitLog(culvertDetailVO.getRecordNumber());
        Integer order=0;
        for (SubmitLogVO submitLogVO:submitLogVOS){
            submitLogVO.setSubmitOrder("第"+(submitLogVOS.size()-order)+"次提交");
            order++;
        }
        culvertDetailVO.setSubmitLogList(submitLogVOS);

        return culvertDetailVO;
    }

    @Autowired
    IFileService fileService;

    @Override
    public List<CulvertExcelVo> importCert(MultipartFile file, HttpServletRequest request) throws IOException {
        List<CulvertExcelVo> vos;
        try (InputStream inputStream = file.getInputStream()) {
            vos = EasyExcel.read(inputStream, CulvertExcelVo.class, new ExcelDataListener<CulvertExcelVo>()).sheet().doReadSync();
        } catch (Exception e) {
            throw new BusinessException(Type.FAIL.getCode(),"excel解析失败 ");
        }
        //解析图片
        Map<String, PictureData> map = ExcelImageUtil.getPictureFromExcel(file, 1);
        if (CollectionUtils.isEmpty(map)) return vos;
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            int index = Integer.parseInt(s) - 1;
            PictureData picData = map.get(s);
            //后缀名
            String extension = picData.suggestFileExtension();
            byte[] data = picData.getData();
            MockMultipartFile file1 = new MockMultipartFile("file",
                    "拍摄场景实景图片" + s + "." + extension,
                    "multipart/form-data",
                    data
            );
            R<FileVO> fileVOR= fileService.upload(file1);
            String body = fileVOR.getData().getFilePath();
            vos.get(index).setShotLocation(body);
        }
        return vos;
    }


    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private UserMessagesDao userMessagesDao;


    @Override
    public void completeTask(ExamineCompleteVO taskCompleteDto) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(taskCompleteDto.getId());
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核信息未找到！");
        }
        if(examinePO.getExamineState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"流程不是待审核状态，请勿重复操作！");
        }
        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(sysUserPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"用户信息未找到！");
        }
        if(examinePO.getExamineUserIds().indexOf(sysUserPO.getId())<0){
            throw new BusinessException(Type.FAIL.getCode(),"当前用户不是该节点的审批人！");
        }
        String EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_AGREE;
        String EXAMINE_ONE_NODE=examinePO.getExamineNode();
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(examinePO.getCreateBy());
        AssociationClapCulvertPO associationClapCulvertPO=associationClapCulvertDao.selectByPrimaryKey(examinePO.getRelatedId());

        if(examinePO.getExamineNode().equals(Constants.EXAMINE_COOPERATION_LETTER[3])&&taskCompleteDto.getOutcome()==1){
            //初审进入下一阶段
            examinePO.setExamineState(2);
            examinePO.setExamineUserIds("");
            examinePO.setExamineNode(Constants.EXAMINE_COOPERATION_LETTER[4]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
        }
        if(examinePO.getExamineNode().equals(Constants.EXAMINE_COOPERATION_LETTER[2])&&taskCompleteDto.getOutcome()==1){
            //初审进入下一阶段
            examinePO.setExamineState(1);
            examinePO.setExamineUserIds(Constants.EXAMINE_3_USER);
            examinePO.setExamineNode(Constants.EXAMINE_COOPERATION_LETTER[3]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);

            new UserMessageThread(userMessagesDao,"1",
                    "贵单位【"+companyInfoPO.getCompanyName()+"】申请协拍的影片【"+associationClapCulvertPO.getMovieName()+"】审查通过，请联系协拍专员雒老师，电话：15223122505，开展后续工作。",
                    companyInfoPO.getId()).start();
        }
        if(examinePO.getExamineNode().equals(Constants.EXAMINE_COOPERATION_LETTER[1])&&taskCompleteDto.getOutcome()==1){
            //审查决定进入下一阶段
            EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_AGREE_S;
            examinePO.setExamineState(1);
            examinePO.setExamineUserIds(Constants.EXAMINE_1_USER);
            examinePO.setExamineNode(Constants.EXAMINE_COOPERATION_LETTER[2]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
        }

        if(taskCompleteDto.getOutcome()!=1){
            log.info("审核失败》》》取消流程::::::::"+taskCompleteDto.getOutcome());
            //审核失败  取消流程
            examinePO.setExamineState(taskCompleteDto.getOutcome()+1);
            examinePO.preUpdate();
            EXAMINE_LOG_AGREE=taskCompleteDto.getOutcome()==2?Constants.EXAMINE_LOG_DISAGREE:Constants.EXAMINE_LOG_RETURN;
            examineDao.updateByPrimaryKeySelective(examinePO);

            if(examinePO.getExamineNode().equals(Constants.EXAMINE_COOPERATION_LETTER[1])){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+associationClapCulvertPO.getMovieName()+"】协拍申请经审核被退回。请联系协拍专员雒老师，电话：15223122505。也可修改后重新提交。",
                        companyInfoPO.getId()).start();
            }
            if(examinePO.getExamineNode().equals(Constants.EXAMINE_COOPERATION_LETTER[2])){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+associationClapCulvertPO.getMovieName()+"】协拍申请经审核被退回。请联系协拍专员雒老师，电话：15223122505。也可修改后重新提交。",
                        companyInfoPO.getId()).start();
            }
        }
        examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), EXAMINE_LOG_AGREE,EXAMINE_ONE_NODE,sysUserPO.getUserName()
                ,sysUserPO.getId(),taskCompleteDto.getComment(),taskCompleteDto.getApprovalFileUrl(),taskCompleteDto.getApprovalFileName(),examinePO.getId()));
    }


    @Override
    public void completeLetter(ExamineLetterVO taskCompleteDto) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(taskCompleteDto.getId());
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核信息未找到！");
        }
        if(examinePO.getExamineState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"流程不是待审核状态，请勿重复操作！");
        }
        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(sysUserPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"用户信息未找到！");
        }
        if(examinePO.getExamineUserIds().indexOf(sysUserPO.getId())<0){
            throw new BusinessException(Type.FAIL.getCode(),"当前用户不是该节点的审批人！");
        }
        String EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_AGREE;
        String EXAMINE_ONE_NODE=examinePO.getExamineNode();
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(examinePO.getCreateBy());

        if(examinePO.getExamineNode().equals(Constants.EXAMINE_COOPERATION_LETTER[3])){
            AssociationClapCulvertPO associationClapCulvertPO=associationClapCulvertDao.selectByPrimaryKey(examinePO.getRelatedId());
            associationClapCulvertPO.setLetterUrl(taskCompleteDto.getApprovalFileUrl());
            associationClapCulvertPO.setLetterName(taskCompleteDto.getApprovalFileName());
            associationClapCulvertPO.preUpdate();
            associationClapCulvertDao.updateByPrimaryKeySelective(associationClapCulvertPO);
            //初审进入下一阶段
            examinePO.setExamineState(2);
            examinePO.setExamineUserIds("");
            examinePO.setExamineNode(Constants.EXAMINE_COOPERATION_LETTER[4]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);

            new UserMessageThread(userMessagesDao,"1",
                    "贵单位【"+companyInfoPO.getCompanyName()+"】提交的电影【"+associationClapCulvertPO.getMovieName()+"】协拍函已完成制件盖章。在“个人中心-办事记录”中查看领取协拍函电子档，也可联系市协拍专员雒老师，电话：15223122505线下领取纸质件。",
                    companyInfoPO.getId()).start();
        }else{
            throw new BusinessException(Type.FAIL.getCode(),"流程未到盖章发函！");
        }

        examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), EXAMINE_LOG_AGREE,EXAMINE_ONE_NODE,sysUserPO.getUserName()
                ,sysUserPO.getId(),"",taskCompleteDto.getApprovalFileUrl(),taskCompleteDto.getApprovalFileName(),examinePO.getId()));
    }



    @Override
    public List<CulvertApprovalDetailExportVo> export(CulvertHarmonizeDto dto) {
        List<CulvertApprovalDetailExportVo> exportVos = new ArrayList<>();
        Example example=new Example(AssociationClapCulvertPO.class);
        example.and().andIn("id",dto.getIds());
        List<AssociationClapCulvertPO> list=associationClapCulvertDao.selectByExample(example);
        for (AssociationClapCulvertPO associationClapCulvertPO:list){
            CulvertApprovalDetailExportVo culvertApprovalDetailExportVo=new CulvertApprovalDetailExportVo();
            culvertApprovalDetailExportVo.setApplicants(associationClapCulvertPO.getProductionUnit());
            culvertApprovalDetailExportVo.setProductionUnit(associationClapCulvertPO.getProductionUnit());
            culvertApprovalDetailExportVo.setCreateDate(associationClapCulvertPO.getCreateDate());
            culvertApprovalDetailExportVo.setMovieName(associationClapCulvertPO.getMovieName());
            culvertApprovalDetailExportVo.setProducer(associationClapCulvertPO.getProducer());
            culvertApprovalDetailExportVo.setProducerPhone(associationClapCulvertPO.getProducerPhone());
            culvertApprovalDetailExportVo.setOutreachProduction(associationClapCulvertPO.getOutreachProduction());
            culvertApprovalDetailExportVo.setOutreachProductionPhone(associationClapCulvertPO.getOutreachProductionPhone());

            Example example1=new Example(AssociationClapDetailPO.class);
            example1.and().andEqualTo("culvertId",associationClapCulvertPO.getId());
            List<AssociationClapDetailPO> deList=associationClapDetailDao.selectByExample(example1);
            StringBuffer district=new StringBuffer();
            List<TAssociationClapDetail> details=new ArrayList<>();
            Date captureStartTime=null;
            Date captureEndTime=null;
            for (AssociationClapDetailPO detailPO:deList){
                TAssociationClapDetail tAssociationClapDetail=new TAssociationClapDetail();
                BeanUtils.copyProperties(detailPO, tAssociationClapDetail);
                district.append(tAssociationClapDetail.getDistrict()).append(",");
                String shotLocation = detailPO.getShotLocation();
                try {
                    // 正则表达式，匹配一个或多个中文字符
                    Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]+");
                    Matcher matcher = pattern.matcher(shotLocation);
                    // 找到并打印所有匹配的中文部分
                    while (matcher.find()) {
                        String group = matcher.group();
                        String encodedUrlPart = URLEncoder.encode(group, "UTF-8");
                        shotLocation = shotLocation.replace(group, encodedUrlPart);
                    }
                    tAssociationClapDetail.setShotLocation(shotLocation);
                } catch (UnsupportedEncodingException e) {
                    log.error("获取图片:{}失败：{}", shotLocation, e.getMessage());
                }
                if(captureStartTime==null){
                    captureStartTime=tAssociationClapDetail.getCaptureStartTime();
                }else{
                    int result = DateUtil.compare(captureStartTime, tAssociationClapDetail.getCaptureStartTime());
                    if(result>0){
                        captureStartTime=tAssociationClapDetail.getCaptureStartTime();
                    }
                }
                if(captureEndTime==null){
                    captureEndTime=tAssociationClapDetail.getCaptureEndTime();
                }else{
                    int result = DateUtil.compare(captureEndTime, tAssociationClapDetail.getCaptureEndTime());
                    if(result>0){
                        captureEndTime=tAssociationClapDetail.getCaptureEndTime();
                    }
                }
                details.add(tAssociationClapDetail);
            }
            culvertApprovalDetailExportVo.setDetails(details);
            culvertApprovalDetailExportVo.setDistrict(district==null?"-":district.substring(0,district.length()-1));
            culvertApprovalDetailExportVo.setCaptureStartTime(captureStartTime);
            culvertApprovalDetailExportVo.setCaptureEndTime(captureEndTime);

            exportVos.add(culvertApprovalDetailExportVo);
        }


        return exportVos;
    }

    @Override
    public void harmonizeCulvert(List<String> ids) {
        for (String id : ids) {
            AssociationClapDetailPO detail = associationClapDetailDao.selectByPrimaryKey(id);
            Integer harmonizeState = detail.getHarmonizeState();
            if (harmonizeState==2) {
                throw new BusinessException(Type.FAIL.getCode(),detail.getDistrict() + "的" + detail.getPosition() + "数据已经是协调状态");
            }
            detail.setHarmonizeState(2);
            associationClapDetailDao.updateByPrimaryKeySelective(detail);
        }
//        tAssociationClapDetailService.updateBatchById(details);
    }


    @Override
    public List<PageCulvertVo> pageCulvert(PageCulvertDto dto) {
        return associationClapCulvertDao.pageCulvert(dto);

    }


}